package com.dylan.magic.redis.config.redis;

import org.springframework.data.redis.core.TimeoutUtils;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * @author : ankang
 * @date : 2019/12/14 21:11
 * @Description :
 */
public class DurationUtils {

    public static TimeData toTimeData(Duration duration) {
        if (TimeoutUtils.hasMillis(duration)) {
            return new TimeData(duration.toMillis(), TimeUnit.MILLISECONDS);
        } else {
            return new TimeData(duration.getSeconds(), TimeUnit.SECONDS);
        }
    }

    public static class TimeData {
        private long time;
        private TimeUnit unit;

        public TimeData(long time, TimeUnit unit) {
            this.time = time;
            this.unit = unit;
        }

        public long getTime() {
            return time;
        }

        public TimeUnit getUnit() {
            return unit;
        }
    }
}
